//
//  SettingViewController.m
//  coba-iOS
//
//  Created by 郭四虎 on 15/12/10.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "SettingViewController.h"
#import "SuggestionViewController.h"
#import "MeDetailViewController.h"
#import <ShareSDKExtension/SSEThirdPartyLoginHelper.h>
#import "NSData+SDDataCache.h"
#import "UMessage.h"

@interface SettingViewController ()

@property (weak, nonatomic) IBOutlet UISwitch *shouldRemote;
@property (weak, nonatomic) IBOutlet UIButton *loginOutbtn;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"系统设置";
    if (USERID) {
        self.loginOutbtn.hidden = NO;
    }else{
        self.loginOutbtn.hidden = YES;
    }
    
    if ([SHOULDREMOTE boolValue]) {
        
        _shouldRemote.on = YES;
    }else{
        
        _shouldRemote.on = NO;
    }
    
}

- (IBAction)cleanCache:(id)sender {
    //清除缓存
    
    //轮播缓存
    [NSData clearCache];
    
    //新闻缓存
    for (NSString *key in [[EGOCache globalCache]allKeys]) {
        //login_id为评论，回复的缓存key
        if (![key isEqualToString:[Myuser sharedUser].login_id]) {
            [[EGOCache globalCache]removeCacheForKey:key];
        }
    }
//    [[EGOCache globalCache]clearCache];
    
    //系统URL缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    //sdimage缓存
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"缓存大小为%.2fM,确定清理？",[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    
}
- (IBAction)systemNoti:(id)sender {
    
    [self.view Message:@"暂时没有消息" HiddenAfterDelay:1];
    //系统设置
}
- (IBAction)giveSuggestion:(id)sender {
    SuggestionViewController *suggestion = [[SuggestionViewController alloc]init];
    suggestion.title = @"意见反馈";
    [self.navigationController pushViewController:suggestion animated:YES];
}

- (IBAction)pushSetting:(UISwitch *)sender {
    //推送设置
    if (sender.on) {
        
        [[NSUserDefaults standardUserDefaults]setObject:@(1) forKey:@"shouldRemote"];
        [UMessage registerDeviceToken:[[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]];
    }else{
        
        [[NSUserDefaults standardUserDefaults]setObject:@(0) forKey:@"shouldRemote"];
        [UMessage unregisterForRemoteNotifications];
    }
    NSLog(@"%d",sender.on);
}

//- (IBAction)checkUpdate:(id)sender {
//    //检查更新
//    if (NEWAPPVER) {
//        [Myuser alertWithTitle:@"前往APPStore更新" Msg:@"确认操作?" target:self action:^{
//            //@"itms-apps://itunes.apple.com/app/id1018221712"
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:NEWAPPVER]];
//        }];
//    }else{
//        [self.view Message:@"已经是最新版本" HiddenAfterDelay:1];
//    }
//    
//}

- (IBAction)loginOut:(id)sender {
    
    if (USERID) {
        NSDictionary *parameter = @{@"login_id":USERID};
        
        [MyAFN POST:LOGINOUT parameters:parameter block:^(id responseObject) {
            NSDictionary *response = responseObject;
            NSLog(@"%@",[response objectForKey:@"status"]);
            if ([[response objectForKey:@"status"]integerValue]) {
                
                [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userid"];
                [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userpwd"];
                [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"username"];
                [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"usericon"];
                
                [[Myuser sharedUser]cleanUser];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeUserinfo" object:nil];
                [self.navigationController popViewControllerAnimated:YES];
                
                [MobClick profileSignOff];
            }else if ([[response objectForKey:@"status"]isEqualToString:@"005"]){
                
                [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userid"];
                [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userpwd"];
                [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"username"];
                [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"usericon"];
                
                [[Myuser sharedUser]cleanUser];
                _loginOutbtn.hidden = YES;
                [self performSelector:@selector(msg) withObject:nil afterDelay:0.5];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeUserinfo" object:nil];
            }
            
        }error:^(NSError *error) {
            
        }fromController:self];

    }
    
    //三方登录取消授权
    if([ShareSDK hasAuthorized:SSDKPlatformTypeSinaWeibo]){
        
        [ShareSDK cancelAuthorize:SSDKPlatformTypeSinaWeibo];
    }
    if ([ShareSDK hasAuthorized:SSDKPlatformTypeQQ]){
        [ShareSDK cancelAuthorize:SSDKPlatformTypeQQ];
    
    }
    if([ShareSDK hasAuthorized:SSDKPlatformTypeWechat]){
        [ShareSDK cancelAuthorize:SSDKPlatformTypeWechat];
    }
    
}

- (void)msg
{
    [self.view Message:@"身份过期，请重新登录" HiddenAfterDelay:1];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 1:
            
            [[SDImageCache sharedImageCache] clearDisk];
            
            break;
            
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
